package com.nbjtjc.safe.service.edu;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.edu.EduCertificateTypeMapper;
import com.nbjtjc.safe.model.edu.EduCertificateType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EduCertificateTypeService {
	@Autowired
	EduCertificateTypeMapper eduCertificateTypeMapper;

	public Result add(EduCertificateType eduCertificateType){
		Result result = new Result();
		
		if(ObjectUtil.isNull(eduCertificateType)||ObjectUtil.isNull(eduCertificateType.getCertName())){
			result.setFalse("证书名称不能为空");
			return result;
		}
		
		int res=0;
		//id为空新增，不为空更新
		if(ObjectUtil.isNull(eduCertificateType.getId())){
			EduCertificateType certificateType = new EduCertificateType();
			certificateType.setCertName(eduCertificateType.getCertName());
			if(CollectionUtil.isNotEmpty(eduCertificateTypeMapper.findByName(certificateType))){
				result.setFalse("已有该证书名称，不能重复");
				return result;
			}
			eduCertificateType.setCancel(0);
			res=eduCertificateTypeMapper.insertSelective(eduCertificateType);
			
			if(res==0){
				result.setFalse("新增证书失败");
				return result;
			}
		}else{
			res=eduCertificateTypeMapper.updateByPrimaryKeySelective(eduCertificateType);
			if(res==0){
				result.setFalse("更新证书失败");
				return result;
			}
		}
		result.setData(eduCertificateType);

		return result;
	}
	
	public Result find(EduCertificateType eduCertificateType){
		Result result = new Result();
	    PageHelper.startPage(eduCertificateType.getPageCurrent(), eduCertificateType.getPageSize());
	    eduCertificateType.setCancel(0);
		List<EduCertificateType> eduCertificateTypes = eduCertificateTypeMapper.search(eduCertificateType);
		result.setData(new PageInfo<>(eduCertificateTypes));
			
		return result;
	}
	
	public Result delete(Integer certTypeId){
		Result result = new Result();
		if(ObjectUtil.isNull(certTypeId)){
			result.setFalse("删除ID不能为空");
			return result;
		}
		EduCertificateType eduCertificateType = eduCertificateTypeMapper.selectByPrimaryKey(certTypeId);
		if(ObjectUtil.isNull(eduCertificateType)){
			result.setFalse("该ID无符合的证书类型");
			return result;
		}else{
			eduCertificateType.setCancel(1);
			int res = eduCertificateTypeMapper.updateByPrimaryKeySelective(eduCertificateType);
			if(res == 0){
				result.setFalse("作废失败");
				return result;
			}
		}
		
		return result;
	}

}
